package com.example.hzq.zqb.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.hzq.zqb.R;
import com.example.hzq.zqb.adapter.CampusshopAdapter;
import com.example.hzq.zqb.bean.CampusShopBean;
import com.example.hzq.zqb.model.Model;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hzq on 2017/7/7.
 */

public class CampusshopFragment extends Fragment{
     private View view=null;
    private Context context;
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager manager;
    private CampusshopAdapter campusAdapter;
    private int page=1;
    private int lastVisibleItemPosition;
    private List<CampusShopBean> list=null;
    private int pageindex;
    public CampusshopFragment(int index){
        pageindex=index;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.campusshopfragment,container,false);
        context=getActivity();
        recyclerView = (RecyclerView)view.findViewById(R.id.campusshop);
        manager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        campusAdapter =new CampusshopAdapter(context);
        campusAdapter.setList(list);
        page=1;
        list=null;
        Model model=new Model();
        Call<List<CampusShopBean>> call;
        if(pageindex==1)
            call= model.info(page);
        else call=model.getGoodsTypeInfo(page,pageindex);
        call.enqueue(new Callback<List<CampusShopBean>>() {
            @Override
            public void onResponse(Call<List<CampusShopBean>> call, Response<List<CampusShopBean>> response) {
                if(response.isSuccessful()&&response.body()!=null)
                {
                    list=response.body();
                    campusAdapter.setList(list);
                }else{
                    Toast.makeText(context,"获取资源失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<CampusShopBean>> call, Throwable t) {
                Toast.makeText(context,"请检查网络连接",Toast.LENGTH_SHORT).show();
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    page+=1;
                    Model model=new Model();
                    Call<List<CampusShopBean>> call;
                    if(pageindex==1)
                        call= model.info(page);
                    else call=model.getGoodsTypeInfo(page,pageindex);
                    call.enqueue(new Callback<List<CampusShopBean>>() {
                        @Override
                        public void onResponse(Call<List<CampusShopBean>> call, Response<List<CampusShopBean>> response) {
                            if(response.isSuccessful()&&response.body()!=null)
                            {
                                list.addAll(response.body());
                                campusAdapter.setList(list);
                            }else{
                                Toast.makeText(context,"获取资源失败", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<List<CampusShopBean>> call, Throwable t) {
                            Toast.makeText(context,"请检查网络连接", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=list.size()-1;
            }
        });
        campusAdapter =new CampusshopAdapter(context);
        campusAdapter.setList(list);
        recyclerView.setAdapter(campusAdapter);
        return view;
    }
}
